package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjGoodsSpecContent;
import com.zb.mapper.ZjGoodsSpecContentMapper;
import com.zb.service.ZjGoodsSpecContentService;
import com.zb.vo.GoodsSpecContentVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品规格内容表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-01
 */
@Service
public class ZjGoodsSpecContentServiceImpl extends ServiceImpl<ZjGoodsSpecContentMapper, ZjGoodsSpecContent> implements ZjGoodsSpecContentService {
    @Resource
    private ZjGoodsSpecContentMapper goodsSpecContentMapper;

    // 保存或修改规格内容
    @Override
    @Transactional
    public void save(Long custAccId, Long specId, List<GoodsSpecContentVo> contentList) {
        Assert.notNull(null != contentList, "规格内容不能为空", null);
        Assert.notEmpty(contentList, "规格内容不能为空", null);
        List<String> v1 = contentList.stream().map(GoodsSpecContentVo::getVal).collect(Collectors.toList());
        Set<String> v2 = contentList.stream().map(GoodsSpecContentVo::getVal).collect(Collectors.toSet());
        Assert.isTrue(v1.size() == v2.size(), "规格内容已重复！", null);

        List<ZjGoodsSpecContent> adds = new ArrayList<>();
        List<ZjGoodsSpecContent> updates = new ArrayList<>();
        ZjGoodsSpecContent content = null;
        for (GoodsSpecContentVo vo : contentList) {
            content = new ZjGoodsSpecContent();
            if (null != vo.getId()) {
                content.setId(vo.getId());
                content.setVal(vo.getVal());
                updates.add(content);
                continue;
            }
            content.setId(IdWorker.getId());
            content.setCustAccId(custAccId);
            content.setSpecId(specId);
            content.setVal(vo.getVal());
            adds.add(content);
        }
        if (!adds.isEmpty()) this.saveBatch(adds);
        if (!updates.isEmpty()) this.updateBatchById(updates);
    }

    // 根据规格ID集合查询规格内容
    @Override
    public List<ZjGoodsSpecContent> getBySpecIdList(List<Long> specIdList) {
        if (null == specIdList || specIdList.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjGoodsSpecContent> wrapper = new QueryWrapper<>();
        wrapper.select("spec_Id", "val");
        wrapper.in("spec_id", specIdList);
        return this.list(wrapper);
    }

    // 根据规格ID获取规格信息
    @Override
    public List<ZjGoodsSpecContent> getBySpecId(Long specId) {
        QueryWrapper<ZjGoodsSpecContent> wrapper = new QueryWrapper<>();
        wrapper.select("id", "val");
        wrapper.eq("spec_id", specId);
        return this.list(wrapper);
    }

    // 根据规格ID删除规格内容
    @Override
    public void deleteBySpecIds(List<Long> specIdList) {
        goodsSpecContentMapper.deleteBySpecIds(specIdList);
    }
}
